/*******************************************************************************
 * 
 ******************************************************************************/
var addUserWin;
var updateUserWin;
var searchUserWin;

var userAddForm;
var userUpdateForm;
var userGrid;

var sellMain;//定义销售单的格式
var u_l_store;
var u_s_store;
var u_sel_ids = [];
var u_sel_name;

var ro_l_store;
var ro_s_store;
var ro_cm;
var ro_sel_ids = [];
var ro_sel_name;

var addResourceWin;
var updateResourceWin;
var resourceAddForm;
var resourceUpdateForm;
var resourceGrid;
var re_l_store;
var re_s_store;
var re_cm;
var re_sel_ids = [];
var re_sel_name;

var pageSize = 5;

Ext.onReady(function() {

	Ext.apply(Ext.form.VTypes, {
				"tel" : function(_v) {

					if (/^\d+$/.test(_v)) {
						return true;
					}

					return false;

				},
				"telText" : "该输入项必须是电话格式,格式如:150...",
				"ageMask" : /[0-9]/i
			});
	/** *************************************************** */
	/**表单的字段的名字对应该字段**/
	FormMapping = Ext.data.Record.create([{
				name : 'id',
				mapping : 'sellMain.id'
			}, {
				name : 'typeCount',
				mapping : 'sellMain.typeCount'
			}, {
				name : 'password',
				mapping : 'sellMain.totalMoney'
			}, {
				name : 'time',
				mapping : 'sellMain.time'
			}, {
				name : 'settlementMethod',
				mapping : 'sellMain.settlementMethod'
			}, {
				name : 'agent',
				mapping : 'sellMain.agent'
			}, {
				name : 'customer',
				mapping : 'sellMain.customer.name'
			}]);

	/** 订单列表的格式 **/
	sellMain = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			new Ext.grid.CheckboxSelectionModel(), {

				header : "编号",

				align : "center",

				dataIndex : "id"

			}, {

				header : "商品类型数",

				align : "center",

				dataIndex : "typeCount"

			}, {

				header : "总金额",

				dataIndex : "totalMoney",

				align : "center"

			}, {

				header : "下单时间",

				dataIndex : "time",

				align : "center"

			}, {

				header : "支付方式",

				dataIndex : "settlementMethod",

				align : "center"

			}, {

				header : "代理人",

				dataIndex : "agent",

				align : "center"

			}, {

				header : "客户",

				dataIndex : "customer",

				align : "center"

			}]);

	u_l_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "userAction!list.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'user_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'name',
										mapping : 'name'
									}, {
										name : 'sex',
										mapping : 'sex'
									}, {
										name : 'tel',
										mapping : 'tel'
									}, {
										name : 'disable',
										mapping : 'disable'
									}]
						}),
				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	userAddForm = new Ext.form.FormPanel({
				url : "userAction!add.action",
				method : 'post',
				defaultType : "textfield",
				labelWidth : 55,
				defaults : {
					anchor : "92%"
				},
				baseCls : "x-plain",
				items : [{
							fieldLabel : "姓名",
							name : "user.name",
							allowBlank : false
						}, {
							xtype : "combo",
							hiddenName : "user.sex",
							fieldLabel : "性别",
							mode : "local",
							displayField : "sex",
							readOnly : true,
							triggerAction : "all",
							value : "男",
							disabled : false,
							editable : false,
							store : new Ext.data.SimpleStore({
										fields : ["sex"],
										data : [["男"],

												["女"]]
									})
						}, {
							fieldLabel : "密码",
							id : "pass1",
							name : "user.password",
							inputType : "password",
							allowBlank : false
						}, {
							fieldLabel : "确认密码",
							id : "pass2",
							vtype : "password",
							confirmTo : "pass1",
							name : "pw",
							inputType : "password",
							allowBlank : false
						}, {
							fieldLabel : "电话",
							vtype : "tel",
							name : "user.tel",
							allowBlank : false
						}, {
							fieldLabel : "状态",
							xtype : "combo",
							hiddenName : "user.disable",
							allowBlank : false,
							value : "1",
							store : new Ext.data.SimpleStore({
										fields : ['index', 'disc'],
										data : [[1, '有效'], [0, '无效']]
									}),
							valueField : 'index',
							displayField : 'disc',
							mode : 'local',
							triggerAction : 'all',
							editable : false
						}]
			});


	addUserWin = new Ext.Window({
		layout : 'fit',
		buttonAlign : 'center',
		width : 330,
		height : 300,
		closeAction : 'hide',
		modal : true,
		plain : true,
		buttons : [{
					text : '保存',
					handler : function() {
						// alert(userAddForm.getForm().url);
						if (userAddForm.getForm().isValid()) {
							userAddForm.getForm().submit({

										success : function(form, action) {
											userAddForm.getForm().reset();
											addUserWin.hide();
											userGrid.getStore().removeAll();
											userGrid.getStore().load({
														params : {
															start : 0,
															limit : pageSize
														}
													});
										},
										failure : function(form, action) {
											Ext.Msg.alert('提示', '原因如下：' + "df");
										},

										waitMsg : '保存数据...'
									});
						}
					}
				}, {
					text : '清空',
					handler : function() {
						userAddForm.getForm().reset();
					}
				}, {
					text : '关闭',
					handler : function() {
						userAddForm.getForm().reset();
						addUserWin.hide();
					}
				}],
		items : [userAddForm]
	});

	u_s_store = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url : "userAction!search.action"
						}),
				reader : new Ext.data.JsonReader({
							totalProperty : 'totalProperty',
							root : 'user_list',
							successProperty : 'SUCCESS',
							fields : [{
										name : 'id',
										mapping : 'id'
									}, {
										name : 'name',
										mapping : 'name'
									}, {
										name : 'sex',
										mapping : 'sex'
									}, {
										name : 'tel',
										mapping : 'tel'
									}, {
										name : 'disable',
										mapping : 'disable'
									}]
						}),

				sortInfo : {
					field : 'id',
					direction : 'ASC'
				}

			});

	userGrid = new Ext.grid.GridPanel({
		renderTo : 'gridPanel',//id
		store : u_l_store,//数据库拿数据
		cm : sellMain,//表单的格式定义
		autoHeight : true,
		title : '销售单信息',
		frame : true,//
		stripeRows : true,//
		tbar : [{
					text : "添加销售单",
					handler : function() {
						addSellMainWin.show();  			//添加销售单，故要弹出添加单的Win
					},
					scope : this
				}, "-", {
					text : "修改销售单",
					handler : function() {
						var sm = userGrid.getSelectionModel();
						if (sm.hasSelection()) {
							updateUserWin.show();
							try {
								var data = userGrid.getSelectionModel()
										.getSelected();
								var myNewRecord = new FormMapping({
											'user.id' : data.get("id"),
											'user.name' : data.get("name"),
											'user.sex' : data.get("sex"),
											'user.tel' : data.get("tel"),
											'user.disable' : data
													.get("disable"),
											'user.password' : data
													.get("password")
										});
								userUpdateForm.getForm()
										.loadRecord(myNewRecord);

							} catch (_err) {
								Ext.Msg.alert("系统提示", _err.description);
								updateUserWin.hide();
							}
						} else {
							Ext.Msg.alert('信息', "请选中一行！");
						}
					},
					scope : this
				}, "-", {
					text : "删除销售单",
					handler : function() {
						var sm = userGrid.getSelectionModel();
						if (sm.hasSelection()) {
							Ext.Msg.confirm('信息', '确定要删除?', function(btn) {
								if (btn == 'yes') {
									var _sr = userGrid.getSelectionModel()
											.getSelections();
											u_sel_ids = [];
									for (var i = 0; i < _sr.length; i++) {
										u_sel_ids[i] = _sr[i].get("id");
									}
									Ext.Ajax.request({
												url : "userAction!delete.action",
												params : {
													selectedList : u_sel_ids
												}
											});
									for (var i = 0; i < _sr.length; i++) {
										userGrid.getStore().remove(userGrid
												.getStore().getAt(_sr[i]
														.get("id")));
									}
									userGrid.getStore().reload({
												params : {
													start : 0,
													limit : pageSize
												}
											});
								} else {
									Ext.Msg.alert('信息', "请选中一行！");
								}
							});
						} else {
							Ext.Msg.alert('信息', "请选中纪录！");
						}
					},
					scope : this
				}, "-", {
					text : "查看销售单",
					handler : function() {
						searchUserWin.show();
					},
					scope : this
				}],
		enableColumnMove : false,
		selModel : new Ext.grid.CheckboxSelectionModel(),
		bbar : new Ext.PagingToolbar({
					pageSize : pageSize,
					store : u_l_store
				})
	});
});